using DataDynamics.ActiveReports;
using DataDynamics.ActiveReports.Export.Xls;
using SS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using TVSBH.func;
namespace TVSGPP.func
{
	public class frmRepProduct : Form
	{
		private IContainer components = null;
		private Button btnPrint;
		private Button btnClose;
		private DataGridView dgvList;
		private Button btnExportToExcel;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
		private DataGridViewTextBoxColumn KipSaveCondition;
		private DataGridViewCheckBoxColumn KipLight;
		private DataGridViewCheckBoxColumn KipWarm;
		private DataGridViewCheckBoxColumn KipChildren;
		private DataGridViewTextBoxColumn possName;
		private Func objFunc = new Func();
		private DataTable objTable = new DataTable();
		private Dictionary<string, string> objSaveCondition = new Dictionary<string, string>();
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle();
			this.btnPrint = new Button();
			this.btnClose = new Button();
			this.dgvList = new DataGridView();
			this.dataGridViewTextBoxColumn1 = new DataGridViewTextBoxColumn();
			this.dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
			this.dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
			this.KipSaveCondition = new DataGridViewTextBoxColumn();
			this.KipLight = new DataGridViewCheckBoxColumn();
			this.KipWarm = new DataGridViewCheckBoxColumn();
			this.KipChildren = new DataGridViewCheckBoxColumn();
			this.possName = new DataGridViewTextBoxColumn();
			this.btnExportToExcel = new Button();
			((ISupportInitialize)this.dgvList).BeginInit();
			base.SuspendLayout();
			this.btnPrint.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.btnPrint.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnPrint.Location = new Point(7, 563);
			this.btnPrint.Name = "btnPrint";
			this.btnPrint.Size = new Size(102, 30);
			this.btnPrint.TabIndex = 47;
			this.btnPrint.Text = "Xem và in [ F7 ]";
			this.btnPrint.UseVisualStyleBackColor = true;
			this.btnPrint.Click += new EventHandler(this.btnPrint_Click);
			this.btnClose.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			this.btnClose.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnClose.Location = new Point(802, 563);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new Size(91, 30);
			this.btnClose.TabIndex = 46;
			this.btnClose.Text = "Thoát [ ESC ]";
			this.btnClose.UseVisualStyleBackColor = true;
			this.btnClose.Click += new EventHandler(this.btnClose_Click);
			this.dgvList.AllowUserToAddRows = false;
			this.dgvList.AllowUserToDeleteRows = false;
			this.dgvList.AllowUserToOrderColumns = true;
			this.dgvList.AllowUserToResizeColumns = false;
			this.dgvList.AllowUserToResizeRows = false;
			dataGridViewCellStyle.BackColor = Color.FromArgb(192, 255, 255);
			dataGridViewCellStyle.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvList.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle;
			this.dgvList.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.dgvList.BackgroundColor = Color.White;
			this.dgvList.BorderStyle = BorderStyle.Fixed3D;
			this.dgvList.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.dgvList.Columns.AddRange(new DataGridViewColumn[]
			{
				this.dataGridViewTextBoxColumn1,
				this.dataGridViewTextBoxColumn2,
				this.dataGridViewTextBoxColumn3,
				this.KipSaveCondition,
				this.KipLight,
				this.KipWarm,
				this.KipChildren,
				this.possName
			});
			this.dgvList.Cursor = Cursors.Hand;
			this.dgvList.EnableHeadersVisualStyles = false;
			this.dgvList.Location = new Point(7, 12);
			this.dgvList.MultiSelect = false;
			this.dgvList.Name = "dgvList";
			this.dgvList.ReadOnly = true;
			this.dgvList.RowHeadersVisible = false;
			this.dgvList.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
			this.dgvList.RowTemplate.DefaultCellStyle.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.dgvList.Size = new Size(886, 546);
			this.dgvList.TabIndex = 49;
			this.dataGridViewTextBoxColumn1.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.dataGridViewTextBoxColumn1.DataPropertyName = "groupName";
			dataGridViewCellStyle2.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
			dataGridViewCellStyle2.NullValue = null;
			this.dataGridViewTextBoxColumn1.DefaultCellStyle = dataGridViewCellStyle2;
			this.dataGridViewTextBoxColumn1.HeaderText = "Nhóm sản phẩm";
			this.dataGridViewTextBoxColumn1.MinimumWidth = 250;
			this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
			this.dataGridViewTextBoxColumn1.ReadOnly = true;
			this.dataGridViewTextBoxColumn1.Width = 250;
			this.dataGridViewTextBoxColumn2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.dataGridViewTextBoxColumn2.DataPropertyName = "productName";
			this.dataGridViewTextBoxColumn2.HeaderText = "Tên sản phẩm";
			this.dataGridViewTextBoxColumn2.MinimumWidth = 150;
			this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
			this.dataGridViewTextBoxColumn2.ReadOnly = true;
			this.dataGridViewTextBoxColumn3.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.dataGridViewTextBoxColumn3.DataPropertyName = "productUnit";
			dataGridViewCellStyle3.Alignment = DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle3.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dataGridViewTextBoxColumn3.DefaultCellStyle = dataGridViewCellStyle3;
			this.dataGridViewTextBoxColumn3.HeaderText = "Đơn vị tính";
			this.dataGridViewTextBoxColumn3.MinimumWidth = 90;
			this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
			this.dataGridViewTextBoxColumn3.ReadOnly = true;
			this.dataGridViewTextBoxColumn3.Width = 90;
			this.KipSaveCondition.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.KipSaveCondition.DataPropertyName = "KipSaveCondition";
			this.KipSaveCondition.HeaderText = "Điều kiện bảo quản";
			this.KipSaveCondition.MinimumWidth = 200;
			this.KipSaveCondition.Name = "KipSaveCondition";
			this.KipSaveCondition.ReadOnly = true;
			this.KipSaveCondition.Width = 200;
			this.KipLight.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.KipLight.DataPropertyName = "KipLight";
			this.KipLight.HeaderText = "Tránh AS";
			this.KipLight.MinimumWidth = 55;
			this.KipLight.Name = "KipLight";
			this.KipLight.ReadOnly = true;
			this.KipLight.Resizable = DataGridViewTriState.True;
			this.KipLight.SortMode = DataGridViewColumnSortMode.Automatic;
			this.KipLight.Width = 55;
			this.KipWarm.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.KipWarm.DataPropertyName = "KipWarm";
			this.KipWarm.HeaderText = "Chống ẩm ướt";
			this.KipWarm.MinimumWidth = 55;
			this.KipWarm.Name = "KipWarm";
			this.KipWarm.ReadOnly = true;
			this.KipWarm.Resizable = DataGridViewTriState.True;
			this.KipWarm.SortMode = DataGridViewColumnSortMode.Automatic;
			this.KipWarm.Width = 55;
			this.KipChildren.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.KipChildren.DataPropertyName = "KipChildren";
			this.KipChildren.HeaderText = "Tránh trẻ em";
			this.KipChildren.MinimumWidth = 55;
			this.KipChildren.Name = "KipChildren";
			this.KipChildren.ReadOnly = true;
			this.KipChildren.Resizable = DataGridViewTriState.True;
			this.KipChildren.SortMode = DataGridViewColumnSortMode.Automatic;
			this.KipChildren.Width = 55;
			this.possName.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.possName.DataPropertyName = "possName";
			this.possName.HeaderText = "Vị trí cất giữ";
			this.possName.MinimumWidth = 120;
			this.possName.Name = "possName";
			this.possName.ReadOnly = true;
			this.possName.Width = 120;
			this.btnExportToExcel.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.btnExportToExcel.Enabled = false;
			this.btnExportToExcel.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnExportToExcel.Location = new Point(110, 563);
			this.btnExportToExcel.Name = "btnExportToExcel";
			this.btnExportToExcel.Size = new Size(102, 30);
			this.btnExportToExcel.TabIndex = 50;
			this.btnExportToExcel.Text = "Xuất file Excel";
			this.btnExportToExcel.UseVisualStyleBackColor = true;
			this.btnExportToExcel.Click += new EventHandler(this.btnExportToExcel_Click);
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(900, 600);
			base.Controls.Add(this.btnExportToExcel);
			base.Controls.Add(this.dgvList);
			base.Controls.Add(this.btnPrint);
			base.Controls.Add(this.btnClose);
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			//base.Icon = (System.Drawing.Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			base.Name = "frmRepProduct";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = "DANH SÁCH THUỐC ĐANG ĐUỢC BÁN";
			base.WindowState = FormWindowState.Maximized;
			base.Load += new EventHandler(this.frmRepGroupProduct_Load);
			base.KeyDown += new KeyEventHandler(this.frmRepGroupProduct_KeyDown);
			((ISupportInitialize)this.dgvList).EndInit();
			base.ResumeLayout(false);
		}
		public frmRepProduct()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
			this.InitializeComponent();
		}
		private void getSaveCondition()
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT * FROM TabSaveCondition";
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				this.objSaveCondition.Add(sqlDataReader["ID"].ToString(), sqlDataReader["Name"].ToString());
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getData()
		{
			try
			{
				this.dgvList.AutoGenerateColumns = false;
				DataTable dataTable = new DataTable();
				DataColumn column = new DataColumn("groupName", typeof(string));
				DataColumn column2 = new DataColumn("productName", typeof(string));
				DataColumn column3 = new DataColumn("productUnit", typeof(string));
				DataColumn column4 = new DataColumn("KipLight", typeof(string));
				DataColumn column5 = new DataColumn("KipWarm", typeof(string));
				DataColumn column6 = new DataColumn("KipChildren", typeof(string));
				DataColumn column7 = new DataColumn("KipSaveCondition", typeof(string));
				DataColumn column8 = new DataColumn("possName", typeof(string));
				dataTable.Columns.Add(column);
				dataTable.Columns.Add(column2);
				dataTable.Columns.Add(column3);
				dataTable.Columns.Add(column4);
				dataTable.Columns.Add(column5);
				dataTable.Columns.Add(column6);
				dataTable.Columns.Add(column7);
				dataTable.Columns.Add(column8);
				string text = "";
				int num = 0;
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT possName, CAST(KipChildren AS BIT) AS KipChildren, KipSaveCondition, CAST(KipLight AS Bit) AS KipLight, Cast(KipWarm AS Bit) AS KipWarm, P_Name,UnitName, Upper(GpName) AS GpName FROM [TabProducts],[TabUnitProduct],[TabGproducts] WHERE [TabProducts].[gpID] = [TabGproducts].[gpID] AND [TabProducts].[P_DonViTinh] = [TabUnitProduct].[UnitID] ORDER BY [GpName]";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					string text2;
					if (num == 0)
					{
						text2 = "";
						text = sqlDataReader["GpName"].ToString();
					}
					else
					{
						text2 = text;
						text = sqlDataReader["GpName"].ToString();
					}
					if (text2.ToUpper().Trim() != text.ToUpper().Trim())
					{
						dataTable.Rows.Add(new object[]
						{
							text,
							" " + sqlDataReader["P_Name"].ToString(),
							" " + sqlDataReader["UnitName"].ToString(),
							sqlDataReader["KipLight"].ToString(),
							sqlDataReader["KipWarm"].ToString(),
							sqlDataReader["KipChildren"].ToString(),
							"  " + this.objSaveCondition[sqlDataReader["KipSaveCondition"].ToString()].ToString(),
							sqlDataReader["possName"].ToString()
						});
					}
					else
					{
						dataTable.Rows.Add(new object[]
						{
							"",
							" " + sqlDataReader["P_Name"].ToString(),
							" " + sqlDataReader["UnitName"].ToString(),
							sqlDataReader["KipLight"].ToString(),
							sqlDataReader["KipWarm"].ToString(),
							sqlDataReader["KipChildren"].ToString(),
							"  " + this.objSaveCondition[sqlDataReader["KipSaveCondition"].ToString()].ToString(),
							sqlDataReader["possName"].ToString()
						});
					}
					num++;
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
				this.objTable = dataTable;
				this.dgvList.DataSource = this.objTable;
				if (this.objTable.Rows.Count > 0)
				{
					this.btnPrint.Enabled = true;
					this.btnExportToExcel.Enabled = true;
				}
				else
				{
					this.btnPrint.Enabled = false;
					this.btnExportToExcel.Enabled = false;
				}
			}
			catch
			{
				this.objTable = null;
				this.dgvList.DataSource = this.objTable;
			}
		}
		private void frmRepGroupProduct_Load(object sender, EventArgs e)
		{
			this.getSaveCondition();
			this.getData();
		}
		private void frmRepGroupProduct_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}
		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		private void btnPrint_Click(object sender, EventArgs e)
		{
			frmRepProductsPrintView frmRepProductsPrintView = new frmRepProductsPrintView();
			frmRepProductsPrint.objTable = this.objTable;
			frmRepProductsPrintView.ShowDialog();
		}
		private void btnExportToExcel_Click(object sender, EventArgs e)
		{
			try
			{
				frmRepProductsPrint.objTable = new DataTable();
				frmRepProductsPrint.objTable = this.objTable;
				ActiveReport3 activeReport = new frmRepProductsPrint();
				activeReport.Run();
				XlsExport xlsExport = new XlsExport();
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				saveFileDialog.ShowDialog();
				string text = saveFileDialog.FileName;
				if (text != "")
				{
					if (!text.ToUpper().Contains(".XLS"))
					{
						text += ".xls";
					}
					xlsExport.Export(activeReport.Document, text);
					MessageBox.Show("Hệ thống đã hoàn thành việc xuất dữ liệu ra file Excel", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				}
				else
				{
					MessageBox.Show("Bạn chưa chọn đường dẫn và tên file cần xuất!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
			catch
			{
			}
		}
	}
}
